﻿using System.Collections.Generic;
using Pyramid.Hardware.Gates;

namespace Pyramid.Hardware.MicroArchitecture
{
    public class Register
    {
        private ILine[] _outputBus;

#if DEBUG
        private ILine[] _internalBus;
#endif

        public Register(IEnumerable<ILine> inputBus, ILine clock, ILine writeFlag)
        {
            var outputBus = new List<ILine>();

#if DEBUG
            var internalBus = new List<ILine>();
#endif

            var clockWithWriteFlag = new AndGate(clock, writeFlag);

            foreach (var line in inputBus)
            {
                var flipFlop = new MasterSlaveFlipFlop(line, clockWithWriteFlag);

#if DEBUG
                internalBus.Add(flipFlop.InternalQ);
#endif

                outputBus.Add(flipFlop.Q);
            }

#if DEBUG
            _internalBus = internalBus.ToArray();
#endif

            _outputBus = outputBus.ToArray();
        }

#if DEBUG
        public IEnumerable<ILine> InternalBus
        {
            get
            {
                return _internalBus;
            }
        }
#endif

        public IEnumerable<ILine> OutputBus
        {
            get
            {
                return _outputBus;
            }
        }
    }
}
